package com.fzy.auth.action;

import org.apache.log4j.Logger;

import com.fzy.auth.entity.User;
import com.fzy.auth.service.UserService;
import com.fzy.core.BaseAction;
import com.fzy.core.MyValidationAware;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class UserSaveAction extends BaseAction implements ModelDriven,
		Preparable, MyValidationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2513266197089818899L;
	private static final Logger log = Logger.getLogger(UserSaveAction.class);
	User user;
	String roleIds;
	UserService userService;
	String errorDispatch;

	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/WEB-INF/jsp/auth/user-input.jsp";
		
		user = (User)validateId(request.getParameter("user.id"), User.class);
		if(hasActionErrors()){
			errorDispatch = "redirect:userList.do";
		}

	}

	public void validate() {
		addFieldError("user.name", "测试");
	}

	public String execute() throws Exception {
		

		// ActionMapping mapping =
		// ActionMapperFactory.getMapper().getMapping(request);
		// ActionValidatorManagerFactory.getInstance().validate(this,
		// mapping.getName());
		// // validate
		// String name = user.getName();
		// if ((name == null) || (name.trim().equals(""))) {
		// addFieldError("name", "You must enter a name.");
		// }
		// if (hasErrors()) {
		// return "dispatcher:/WEB-INF/jsp/auth/user-input.jsp";
		// }
		// if(this.hasErrors()){
		// return "dispatcher:/WEB-INF/jsp/auth/user-input.jsp";
		// }

		userService.saveEntityId(user);

		return "redirect:/auth/userList.do";
	}

	public Object getModel() {
		return user;
	}

	public User getUser() {
		return user;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}
	
}
